<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title> 22 </title>
	
<link rel="stylesheet" href="zero.css" />
<script src="https://webapi.amap.com/maps?v=1.3&key=d3681724444d030e6a5020b5565c61b9"></script>

</head>
<body onLoad="mapInit()">
	
	<!---->

<div id="iCenter"></div>
<div id="iControlbox">
    <ul>
        <li>
            <button onclick="javascript:getCurrentPosition();">浏览器定位</button>
        </li>
        <li>
            <button onclick="javascript:showCityInfo();">IP定位</button>
        </li>
        <li>
            <input type="text" id="key_1" value="上海市" />
            <button onclick="javascript:placeSearch();">关键字定位</button>
        </li>
        <li>
            <input type="text" id="key_2" value="北京市朝阳区大屯路" />
            <button onclick="javascript:geocoder();">地址定位</button>
        </li>
    </ul>
</div>
<div id="result"></div>

<script language="javascript">
var mapObj, geolocation;
var result;
function mapInit () {
    mapObj = new AMap.Map('iCenter');    //默认定位：初始化加载地图时，center及level属性缺省，地图默认显示用户所在城市范围
};
function getCurrentPosition () { //调用浏览器定位服务
mapObj.plugin('AMap.Geolocation', function () {
    geolocation = new AMap.Geolocation({
        enableHighAccuracy: true,//是否使用高精度定位，默认:true
        timeout: 10000,          //超过10秒后停止定位，默认：无穷大
        maximumAge: 0,           //定位结果缓存0毫秒，默认：0
        convert: true,           //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
        showButton: true,        //显示定位按钮，默认：true
        buttonPosition: 'LB',    //定位按钮停靠位置，默认：'LB'，左下角
        buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
        showMarker: true,        //定位成功后在定位到的位置显示点标记，默认：true
        showCircle: true,        //定位成功后用圆圈表示定位精度范围，默认：true
        panToLocation: true,     //定位成功后将定位到的位置作为地图中心点，默认：true
        zoomToAccuracy:true      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
    });
    mapObj.addControl(geolocation);
    AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
    AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
    });
};
function onComplete (data) {   //解析定位结果
var str = '<p>定位成功</p>';
str += '<p>经度：' + data.position.getLng() + '</p>';
str += '<p>纬度：' + data.position.getLat() + '</p>';
str += '<p>精度：' + data.accuracy + ' 米</p>';
str += '<p>是否经过偏移：' + (data.isConverted ? '是' : '否') + '</p>';
    result.innerHTML = str;
};
function onError (data) {    //解析定位错误信息
var str = '<p>定位失败</p>';
str += '<p>错误信息：'
switch(data.info) {
    case 'PERMISSION_DENIED':
        str += '浏览器阻止了定位操作';
        break;
    case 'POSITION_UNAVAILBLE':
        str += '无法获得当前位置';
        break;
    case 'TIMEOUT':
        str += '定位超时';
        break;
    default:
        str += '未知错误';
        break;
}
str += '</p>';
    result.innerHTML = str;
};
function showCityInfo() {  //IP定位
//加载IP定位插件
mapObj.plugin(["AMap.CitySearch"], function() {
    //实例化城市查询类
    var citysearch = new AMap.CitySearch();
    //自动获取用户IP，返回当前城市
    citysearch.getLocalCity();
    AMap.event.addListener(citysearch, "complete", function(result){
        if(result && result.city && result.bounds) {
            var cityinfo = result.city;
            var citybounds = result.bounds;
            document.getElementById('result').innerHTML = "您当前所在城市："+cityinfo+"";
            //地图显示当前城市
            mapObj.setBounds(citybounds);
        }
        else {
            document.getElementById('result').innerHTML = "您当前所在城市："+result.info+"";
        }
    });
    AMap.event.addListener(citysearch, "error", function(result){alert(result.info);});
    });
}
var MSearch;
var key_1;
function placeSearch() {  //POI搜索，关键字查询
key_1 = document.getElementById("key_1").value;
document.getElementById('result').innerHTML = "您输入的是：" + key_1;
mapObj.plugin(["AMap.PlaceSearch"], function() {    //构造地点查询类  
    MSearch = new AMap.PlaceSearch({ 
        pageSize:10,
        pageIndex:1,
        city:"021" //城市
    });
    AMap.event.addListener(MSearch, "complete", function(data){
        var poiArr = data.poiList.pois;
        var lngX = poiArr[0].location.getLng();
        var latY = poiArr[0].location.getLat();
        mapObj.setCenter(new AMap.LngLat(lngX, latY));
    });//返回地点查询结果        
    MSearch.search(key_1); //关键字查询
    });
}
var MGeocoder;
var key_2;
function geocoder() {  //地理编码返回结果展示
key_2 = document.getElementById("key_2").value;  
document.getElementById('result').innerHTML = "您输入的是：" + key_2;    
mapObj.plugin(["AMap.Geocoder"], function() {     //加载地理编码插件
    MGeocoder = new AMap.Geocoder({
        city:"010", //城市，默认：“全国”
        radius:1000 //范围，默认：500
    });
    //返回地理编码结果
    AMap.event.addListener(MGeocoder, "complete", function(data){
        var geocode = data.geocodes; 
        var lngX = geocode[0].location.getLng();
        var latY = geocode[0].location.getLat();
        mapObj.setCenter(new AMap.LngLat(lngX, latY));
    });        
    MGeocoder.getLocation(key_2);  //地理编码
    });
} 
</script>

<!---->
</body>
</html>





